package com.wayin.lift.face;

import com.arcsoft.face.*;
import com.arcsoft.face.enums.DetectMode;
import com.arcsoft.face.enums.DetectOrient;
import com.arcsoft.face.enums.ErrorInfo;
import com.arcsoft.face.enums.ImageFormat;
import com.arcsoft.face.toolkit.ImageInfo;
import com.wayin.lift.entity.PersonnelEntity;
import org.springframework.util.ClassUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static com.arcsoft.face.toolkit.ImageFactory.getRGBData;

public class FaceCheck {

	public static String appId = "3D6r7vWu9TNQoZYN1KWrkY5uEJEhL1FbRdyPgSsBoVRT";

	public static String sdkKey = "7WnpvCEYeG9SbSprd9VQihhmkAaDAErVpuM3uKB76bGK";

	public static FaceEngine faceEngine = new FaceEngine("C:\\Users\\Administrator\\Desktop\\emmmmm\\libs\\WIN64");
//	public static FaceEngine faceEngine = new FaceEngine("C:\\Users\\Administrator\\Desktop\\WIN64");
	// 激活引擎
	public static int activeCode = faceEngine.activeOnline(appId, sdkKey);

	static {
		if (activeCode != ErrorInfo.MOK.getValue() && activeCode != ErrorInfo.MERR_ASF_ALREADY_ACTIVATED.getValue()) {
			System.out.println("引擎激活失败");
		} else {
			System.out.println("引擎激活成功");
		}

		// 引擎配置
		EngineConfiguration engineConfiguration = new EngineConfiguration();
		engineConfiguration.setDetectMode(DetectMode.ASF_DETECT_MODE_IMAGE);
		engineConfiguration.setDetectFaceOrientPriority(DetectOrient.ASF_OP_0_ONLY);

		// 功能配置
		FunctionConfiguration functionConfiguration = new FunctionConfiguration();
		functionConfiguration.setSupportAge(true);
		functionConfiguration.setSupportFace3dAngle(true);
		functionConfiguration.setSupportFaceDetect(true);
		functionConfiguration.setSupportFaceRecognition(true);
		functionConfiguration.setSupportGender(true);
		functionConfiguration.setSupportLiveness(true);
		functionConfiguration.setSupportIRLiveness(true);
		engineConfiguration.setFunctionConfiguration(functionConfiguration);

		// 初始化引擎
		int initCode = faceEngine.init(engineConfiguration);
		if (initCode != ErrorInfo.MOK.getValue()) {
			System.out.println("初始化引擎失败");
		} else {
			System.out.println("初始化引擎成功");
		}

	}
	
	public static double face(String face1,String face2) {
		
        //人脸检测
        ImageInfo imageInfo = getRGBData(new File(face1));
        List<FaceInfo> faceInfoList = new ArrayList<FaceInfo>();
        int detectCode = faceEngine.detectFaces(imageInfo.getImageData(), imageInfo.getWidth(), imageInfo.getHeight(), ImageFormat.CP_PAF_BGR24, faceInfoList);
        System.out.println(faceInfoList);

        if (faceInfoList.size() == 0){
        	return 0;
		}
        
        //特征提取
        FaceFeature faceFeature = new FaceFeature();
        int extractCode = faceEngine.extractFaceFeature(imageInfo.getImageData(), imageInfo.getWidth(), imageInfo.getHeight(), ImageFormat.CP_PAF_BGR24, faceInfoList.get(0), faceFeature);
        
        System.out.println("特征值大小：" + faceFeature.getFeatureData());
        for (byte b : faceFeature.getFeatureData()){
			System.out.print(b);
		}

        //人脸检测2
        ImageInfo imageInfo2 = getRGBData(new File(face2));
        List<FaceInfo> faceInfoList2 = new ArrayList<FaceInfo>();
        int detectCode2 = faceEngine.detectFaces(imageInfo2.getImageData(), imageInfo2.getWidth(), imageInfo2.getHeight(), ImageFormat.CP_PAF_BGR24, faceInfoList2);
        System.out.println(faceInfoList2);

        if (faceInfoList2.size() == 0){
        	return 0;
		}

        //特征提取2
        FaceFeature faceFeature2 = new FaceFeature();
        int extractCode2 = faceEngine.extractFaceFeature(imageInfo2.getImageData(), imageInfo2.getWidth(), imageInfo2.getHeight(), ImageFormat.CP_PAF_BGR24, faceInfoList2.get(0), faceFeature2);
        System.out.println("特征值大小：" + faceFeature2.getFeatureData());

        String a = "00-6680011667-931231-6781-403361-7-3-94-6890119-4661-477-101-6779461560116-946361-121-77-66-68-75417661-124-11-11961-62-695061-70100-70-68-4638114-67105-91-55616524-9660644-41591043964626311814-66418-9261-43158461124138162760-9-684234-6260-27-26-34-68-118112-20-6872-3861-2575-2461-127117-3760-115-7850599114-24-6723-78-66-67-9122461102-23-1286190-125-336168-27-88-6891753861-93-44-2361110-8040-67125902861-99-2917-679115-100-683911898-6710897526147926061-87104-12060-20-515-67114-38-115-6872-61-27-6873-49-90-6812109686182163606011974-6754-1224-67558286601081610361-15281176039-114115-6893-4561-87811460-64-21-109-671143140-671298-67-47-6771-6795113-6661111-1079061-46332561-53-6-12361-72-4762-67-107-124-80-67-684-47-68-94118-48611119106112336111-6784-125406171-36-95-67-1193431-66-94-14-104-67098-110-67-10755-128-67-56110-6159-2187-79-67636034-67121154260-5822-12061-21127-81-67-27-348861-63-12611-689556-125-6755-56-88-67-84-12759-67104220-673315-105-67-1854-26-69-104-111-926187-44-3-6976-23-5592122-4261-43732361-12257-76-67-65-45-118-6823-12367-671712656-67-748-83-67-6781-17-6797124105-67422797-68-59-60-97-671756376112185121611116196-67-8-126116-68-76-52356061-36-124608920176232-3029615741-436034-473861-21-99-65-673-59-112-67-124-83-4160477-206046-1510061-92-1311617928-116-68-104-916-67139812761-9917-99-67910-55-67-61-68-12361-116-65516144-126-4661-3-353561-4946120-6819-59156161-56-97-67634756193-4048-6876111-11659-101385661-111-100-127-67-77-48-5061-20-32959-68120-10261-9107-30-6895-75-67-68-5061611858-296116-8411261-17-10-100-68-12880-45-6865137661-435987601056806079-109-2761-1196961-4454-117-67116-40862-159010761-17-521061-69-128-39-676728-5861118-9483-67107-114-3-6884-7820-67-4217123-6966-121-6-685985-5461-10366-4861123-63-81-68-109-18-8761-5-91-66-67121-25-127-671266236-67116-246461-45-58-18-67-682-126-68-92-111-114-67-111-88546093-16-7360-7594-861-103-1-123-67929653-67127-1464-68-84-31-6848-2-7861-11540-43-69-15722661-34-12795-6826-12047-67115103119-69104-116-4260-103-121113-67-92-22120-67-7672-716044-34-6461-1-38962579-245943-15-10561-102-22-18603212613-6674-86-6960-33-14-9460-13-59-89614293-100-6972-1-6360-5525-9460-2526-127-67-14-43-123-6746-6-118-6789112-12761-56-52-10959-619-4860-11050110607795112-67-8198-127-67-86-86-24-68579-3-68901001076180456761610-3760-44-91-118-68-80641-67-98-81-986173-47-55-678-1691-71-7323102-67-60-11571-68-124-1027661113421361-114115-106-67-97-61-8061-46-118-85614-1164661-3839127-6827-2257-69-96-6081-67-85-708-67111-81-626021-67-117-699397-90617473-12761-119-631-68-2581-24597544-1166087-109109-681238878-6786-7575-68-7-5077-66-76-10-39-67660-9461614-20-6834-947061-42-2415-67-9681-1960-98-411-6780-123-11261[com.arcsoft.face.Rect(46, 73 - 302, 328),1, com.arcsoft.face.Rect(24, 158 - 45, 179),1]";
		byte[] b = new byte[a.length()];
		for (int i = 0; i < a.length() -1; i++){
        	b[i] = (byte) a.indexOf(i);
		}
        //特征比对
        FaceFeature targetFaceFeature = new FaceFeature();
        targetFaceFeature.setFeatureData(b);
        FaceFeature sourceFaceFeature = new FaceFeature();
        sourceFaceFeature.setFeatureData(faceFeature2.getFeatureData());
        FaceSimilar faceSimilar = new FaceSimilar();
        int compareCode = faceEngine.compareFaceFeature(targetFaceFeature, sourceFaceFeature, faceSimilar);
        //System.out.println("相似度：" + faceSimilar.getScore());
        
        //引擎卸载
        int unInitCode = faceEngine.unInit();
		System.out.println(faceSimilar.getScore());
        return faceSimilar.getScore();
	}

	public static PersonnelEntity face2(List<PersonnelEntity> personnel, String face2) throws IOException {

		String path1 = ClassUtils.getDefaultClassLoader().getResource("static").getPath();
		//人脸检测2
		ImageInfo imageInfo2 = getRGBData(new File(face2));
		List<FaceInfo> faceInfoList2 = new ArrayList<FaceInfo>();
		int detectCode2 = faceEngine.detectFaces(imageInfo2.getImageData(), imageInfo2.getWidth(), imageInfo2.getHeight(), ImageFormat.CP_PAF_BGR24, faceInfoList2);

		if (faceInfoList2.size() == 0){
			return null;
		}

		//特征提取2
		FaceFeature faceFeature2 = new FaceFeature();
		int extractCode2 = faceEngine.extractFaceFeature(imageInfo2.getImageData(), imageInfo2.getWidth(), imageInfo2.getHeight(), ImageFormat.CP_PAF_BGR24, faceInfoList2.get(0), faceFeature2);

		ImageInfo imageInfo = new ImageInfo();
		List<FaceInfo> faceInfoList = new ArrayList<FaceInfo>();
		FaceFeature faceFeature = new FaceFeature();
		FaceFeature targetFaceFeature = new FaceFeature();
		FaceFeature sourceFaceFeature = new FaceFeature();
		FaceSimilar faceSimilar = new FaceSimilar();

		for (PersonnelEntity p : personnel){
			long start=System.currentTimeMillis();   //获取开始时间
			if (p.getPicture() == null){
				continue;
			}
			//人脸检测
//			File file = new File();
			imageInfo = getRGBData(new File(path1 + p.getPicture()));
			long end=System.currentTimeMillis(); //获取结束时间
			System.out.println("程序运行时间： "+(end-start)+"ms");
			int detectCode = faceEngine.detectFaces(imageInfo.getImageData(), imageInfo.getWidth(), imageInfo.getHeight(), ImageFormat.CP_PAF_BGR24, faceInfoList);
			System.out.println(faceInfoList);

			//特征提取
			int extractCode = faceEngine.extractFaceFeature(imageInfo.getImageData(), imageInfo.getWidth(), imageInfo.getHeight(), ImageFormat.CP_PAF_BGR24, faceInfoList.get(0), faceFeature);

			System.out.println("特征值大小：" + faceFeature.getFeatureData().length);

			//特征比对
			targetFaceFeature.setFeatureData(faceFeature.getFeatureData());
			sourceFaceFeature.setFeatureData(faceFeature2.getFeatureData());
			int compareCode = faceEngine.compareFaceFeature(targetFaceFeature, sourceFaceFeature, faceSimilar);
			//System.out.println("相似度：" + faceSimilar.getScore());

			//引擎卸载
			if (faceSimilar.getScore() > 0.8){
				faceEngine.unInit();
				return p;
			} else {
				continue;
			}
		}
		faceEngine.unInit();
		return null;
	}

	public static PersonnelEntity face3(List<ImageInfo> imageInfos, List<PersonnelEntity> personnel, String face2) throws IOException {

		String path1 = ClassUtils.getDefaultClassLoader().getResource("static").getPath();
		//人脸检测2
		ImageInfo imageInfo2 = getRGBData(new File(face2));
		List<FaceInfo> faceInfoList2 = new ArrayList<FaceInfo>();
		int detectCode2 = faceEngine.detectFaces(imageInfo2.getImageData(), imageInfo2.getWidth(), imageInfo2.getHeight(), ImageFormat.CP_PAF_BGR24, faceInfoList2);

		if (faceInfoList2.size() == 0){
			return null;
		}

		//特征提取2
		FaceFeature faceFeature2 = new FaceFeature();
		int extractCode2 = faceEngine.extractFaceFeature(imageInfo2.getImageData(), imageInfo2.getWidth(), imageInfo2.getHeight(), ImageFormat.CP_PAF_BGR24, faceInfoList2.get(0), faceFeature2);

//		ImageInfo imageInfo = new ImageInfo();
		List<FaceInfo> faceInfoList = new ArrayList<FaceInfo>();
		FaceFeature faceFeature = new FaceFeature();
		FaceFeature targetFaceFeature = new FaceFeature();
		FaceFeature sourceFaceFeature = new FaceFeature();
		FaceSimilar faceSimilar = new FaceSimilar();

		for (ImageInfo imageInfo : imageInfos){
			long start=System.currentTimeMillis();   //获取开始时间
			//人脸检测
//			File file = new File();
//			imageInfo = getRGBData(new File(path1 + p.getPicture()));

			int detectCode = faceEngine.detectFaces(imageInfo.getImageData(), imageInfo.getWidth(), imageInfo.getHeight(), ImageFormat.CP_PAF_BGR24, faceInfoList);
			System.out.println(faceInfoList);

			//特征提取
			int extractCode = faceEngine.extractFaceFeature(imageInfo.getImageData(), imageInfo.getWidth(), imageInfo.getHeight(), ImageFormat.CP_PAF_BGR24, faceInfoList.get(0), faceFeature);

			System.out.println("特征值大小：" + faceFeature.getFeatureData().length);

			//特征比对
			targetFaceFeature.setFeatureData(faceFeature.getFeatureData());
			sourceFaceFeature.setFeatureData(faceFeature2.getFeatureData());
			int compareCode = faceEngine.compareFaceFeature(targetFaceFeature, sourceFaceFeature, faceSimilar);
			//System.out.println("相似度：" + faceSimilar.getScore());
			long end=System.currentTimeMillis(); //获取结束时间
			System.out.println("程序运行时间： "+(end-start)+"ms");
			//引擎卸载
			if (faceSimilar.getScore() > 0.8){
				faceEngine.unInit();
				PersonnelEntity p = personnel.get(imageInfos.indexOf(imageInfo));
				return p;
			} else {
				continue;
			}
		}
		faceEngine.unInit();
		return null;
	}

	public static List<FaceFeature> faceFeatures = new ArrayList<>();

	public static void face4(List<PersonnelEntity> personnel) throws Exception {
		String path1 = ClassUtils.getDefaultClassLoader().getResource("static/face").getPath();
		faceFeatures.clear();
		for (PersonnelEntity p : personnel){
			long start=System.currentTimeMillis();   //获取开始时间
			if (p.getPicture() == null){
				faceFeatures.add(null);
				continue;
			}
			ImageInfo imageInfo = new ImageInfo();
			//人脸检测
			imageInfo = getRGBData(new File(path1 + p.getPicture()));
			long end=System.currentTimeMillis(); //获取结束时间
			System.out.println("程序运行时间： "+(end-start)+"ms");
			List<FaceInfo> faceInfoList = new ArrayList<FaceInfo>();
			int detectCode = faceEngine.detectFaces(imageInfo.getImageData(), imageInfo.getWidth(), imageInfo.getHeight(), ImageFormat.CP_PAF_BGR24, faceInfoList);
			System.out.println(faceInfoList);
			if (faceInfoList.size() == 0){
				throw new Exception();
			}

			//特征提取
			FaceFeature faceFeature = new FaceFeature();
			int extractCode = faceEngine.extractFaceFeature(imageInfo.getImageData(), imageInfo.getWidth(), imageInfo.getHeight(), ImageFormat.CP_PAF_BGR24, faceInfoList.get(0), faceFeature);

			faceFeatures.add(faceFeature);
		}
//		faceEngine.unInit();
	}

	public static PersonnelEntity face5(List<PersonnelEntity> personnel, File file) throws Exception {
//		String path1 = ClassUtils.getDefaultClassLoader().getResource("static").getPath();
		//人脸检测2
		long start1=System.currentTimeMillis();   //获取开始时间
		ImageInfo imageInfo2 = getRGBData(file);
		List<FaceInfo> faceInfoList2 = new ArrayList<FaceInfo>();
		int detectCode2 = faceEngine.detectFaces(imageInfo2.getImageData(), imageInfo2.getWidth(), imageInfo2.getHeight(), ImageFormat.CP_PAF_BGR24, faceInfoList2);

		if (faceInfoList2.size() == 0) {
			return null;
		}

		//特征提取2
		FaceFeature faceFeature2 = new FaceFeature();
		int extractCode2 = faceEngine.extractFaceFeature(imageInfo2.getImageData(), imageInfo2.getWidth(), imageInfo2.getHeight(), ImageFormat.CP_PAF_BGR24, faceInfoList2.get(0), faceFeature2);
		long end1=System.currentTimeMillis(); //获取结束时间
		System.out.println("程序运行时间： "+(end1-start1)+"ms");
		for (FaceFeature f : faceFeatures) {
			long start=System.currentTimeMillis();   //获取开始时间
			FaceFeature targetFaceFeature = new FaceFeature();
			FaceFeature sourceFaceFeature = new FaceFeature();
			FaceSimilar faceSimilar = new FaceSimilar();
			//特征比对
			targetFaceFeature.setFeatureData(f.getFeatureData());
			sourceFaceFeature.setFeatureData(faceFeature2.getFeatureData());
			int compareCode = faceEngine.compareFaceFeature(targetFaceFeature, sourceFaceFeature, faceSimilar);

			long end=System.currentTimeMillis(); //获取结束时间
			System.out.println("程序运行时间： "+(end-start)+"ms");
			System.out.println(faceSimilar.getScore());
			//引擎卸载
			if (faceSimilar.getScore() > 0.8){
//				faceEngine.unInit();
				PersonnelEntity p = personnel.get(faceFeatures.indexOf(f));
				return p;
			} else {
				continue;
			}
		}
//		faceEngine.unInit();
		throw new Exception("noFace");
	}

}
